
package k8s

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type secretData struct {
	Visible	bool					`json:"visible"`
	Len int							`json:"length"`
	Content string					`json:"content"`   //TODO:api接口返回数据如何加密？
}


type K8sSecret struct {
	Name string						`json:"name"`
	CreateDate string				`json:"createdate"`
	Labels map[string]string		`json:"labels"`
	Annotation map[string]string	`json:"annotation"`
	Namespace string				`json:"namespace"`
	Data map[string]secretData		`json:"data"`

}

func GetSecretDetail(ns string,name string)(k8Ssecert K8sSecret,err error) {
	secret, _ := clientset.CoreV1().Secrets(ns).Get(name,metav1.GetOptions{})

	k8Ssecert = K8sSecret{
		Name: secret.Name,
		Namespace: secret.Namespace,
		Annotation: secret.Annotations,
		CreateDate: secret.CreationTimestamp.Format("2006-01-02"),

		Data: func()( map[string]secretData){
			result := make(map[string]secretData)
			for k,v := range secret.Data {
				result[k] =  secretData {
					Len: len(v),
					Content: string(v),
				}
			}
			return result
		}(),

	}
	return
}

func getSecretList(ns string)(k8Secrets []K8sSecret,err error) {
	secrets, _ := clientset.CoreV1().Secrets(ns).List(metav1.ListOptions{})

	for _,item := range secrets.Items {

		k8Secrets = append(k8Secrets,K8sSecret{
			Name: item.Name,
			Labels: item.Labels,
			CreateDate: item.CreationTimestamp.Format("2006-01-02"),
		})
	}

	return
}